package com.heima.admin.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.admin.mapper.SensitiveMapper;
import com.heima.admin.service.SensitiveService;
import com.heima.model.admin.dtos.SensitiveDTO;
import com.heima.model.admin.pojos.AdSensitive;
import com.heima.model.common.dtos.PageResponseResult;
import com.heima.model.common.dtos.ResponseResult;
import com.heima.model.common.enums.AppHttpCodeEnum;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class SensitiveServiceImpl extends ServiceImpl<SensitiveMapper, AdSensitive> implements SensitiveService {
    @Autowired
    private SensitiveMapper sensitiveMapper;
    /**
     * 分页查询敏感词
     * @param dto
     * @return
     */
    @Override
    public ResponseResult findListByQuery(SensitiveDTO dto) {
        //1.校验业务参数
        //检查分页参数
        dto.checkParam();
        //2.执行分页条件查询
        //2.1分页条件对象
        Page<AdSensitive> pageReq = new Page<>(dto.getPage(), dto.getSize());
        //2.2查询条件
        LambdaQueryWrapper<AdSensitive> wrapper = Wrappers.<AdSensitive>lambdaQuery();
        //判断前端是否传sensitives  如果有值   添加模糊条件查询
        if(StringUtils.isNotBlank(dto.getName())){
            wrapper.like(AdSensitive::getSensitives,dto.getName());
        }
        //执行查询
        IPage<AdSensitive> page = page(pageReq, wrapper);
        //封装响应结果
        PageResponseResult result = new PageResponseResult(dto.getPage(), dto.getSize(), page.getTotal(), page.getRecords());
        return result;
    }

    /**
     * 新增敏感词
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult insert(AdSensitive adSensitive) {
        //校验参数
        if(adSensitive == null||StringUtils.isBlank(adSensitive.getSensitives())){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"参数不能为空");
        }
        //判断是否存在
        int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, adSensitive.getSensitives()));
        if(count>0){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"敏感词已存在");
        }
        //保存到数据库
        adSensitive.setCreatedTime(new Date());
        this.save(adSensitive);
        //返回封装结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 修改数据
     * @param adSensitive
     * @return
     */
    @Override
    public ResponseResult update(AdSensitive adSensitive) {
        //参数校验
        if(adSensitive.getId()==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id为空");
        }
        AdSensitive oldSensitive = this.getById(adSensitive.getId());
        String newname = adSensitive.getSensitives();
        //判断敏感词是否为空，或者值已存在
        if(StringUtils.isNotBlank(newname)&&!newname.equals(oldSensitive.getSensitives())){
            //判断数据名称是否已存在
            int count = this.count(Wrappers.<AdSensitive>lambdaQuery().eq(AdSensitive::getSensitives, newname));
            if(count>0){
                return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"频道已存在");
            }
        }
        //2.根据id修改数据
        this.updateById(adSensitive);
        //3.返回封装结果
        return ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 根据id删除敏感词
     * @param id
     * @return
     */
    @Override
    public ResponseResult delete(Integer id) {
        //参数校验
        if(id == null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"id不能为空");
        }
        //判断id是否有数据
        AdSensitive byId = this.getById(id);
        if(byId==null){
            return ResponseResult.errorResult(AppHttpCodeEnum.PARAM_INVALID,"对应的敏感词信息不存在");
        }
        //删除敏感词
        this.removeById(id);
        //封装响应结果
        return  ResponseResult.okResult(AppHttpCodeEnum.SUCCESS);
    }

    /**
     * 查询敏感词内容列表
     * @return
     */
    @Override
    public ResponseResult<List<String>> selectAllSensitives() {
        List<String> allSensitives = sensitiveMapper.findAllSensitives();
        return ResponseResult.okResult(allSensitives);
    }
}
